package dao;

import data.DataConnection;
import entity.Category;
import utils.DateUtil;

import java.util.List;



public class CategoryDao {

    /**

     * 根据id删除删除相应分类

     * @param id

     * @return

     */

    public  static boolean   deleteByPrimaryKey(Integer id){

        //创建sql命令

        String sql="delete from category  where id=? ";

        //给sql命令中的参数赋值

        Object[] para=new Object[]{id};

        //执行sql命令成功返回true，否则返回false

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 根据父分类id删除相应子分类，即当删除一个分类，则也要删除它的子分类

     * @param parentId

     * @return

     */

    public  static boolean   deleteByParentId(Integer parentId){

        //创建sql命令

        String sql="delete from category  where parent_id=? ";

        //给sql命令赋值

        Object[] para=new Object[]{parentId};

        //执行sql命令

        if(DataConnection.request(sql,para)){

            //命令执行成功返回true

            return true;

        }

        //命令执行不成功返回false

        return false;

    }



    /**

     * 添加分类

     * @param record 分类信息

     * @return 添加成功返回true,否则返回false

     */

    public  static boolean   insert(Category record){

        String sql="insert into category(" +

                "parent_id, " +

                "name, " +

                "status," +

                "level, " +

                "create_time, " +

                "update_time) " +

                "values( " +

                "?,?,?,?,?,?)";

        Object[] para=new Object[]{record.getParent_id(),

                record.getName(),

                record.getStatus(),

                record.getLevel(),

                DateUtil.getSystemTime(),

                DateUtil.getSystemTime()};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 根据id更新分类

     * @param record 更新分类信息

     * @return 更新成功返回true,否则返回false

     */

    public  static boolean   updateByPrimaryKey(Category record){

        String sql="update  category set " +

                "parent_id=?, " +

                "name=?, " +

                "status=?, " +

                "level=?, " +

                "update_time=? " +

                " where id=?" ;

        Object[] para=new Object[]{

                record.getParent_id(),

                record.getName(),

                record.getStatus(),

                record.getLevel(),

                DateUtil.getSystemTime(),

                record.getId()};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    public static boolean updateLevelById(int id,int level){

        String sql="update category set level=? where id=?";

        Object[] para=new Object[]{level,id};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }



    /**

     * 根据id值，修改相应记录的父类parent_id和分类级别level

     * @param parent_id 父类的id

     * @param level 本记录中的分类级别

     * @param id 记录的主关键字

     * @return 成功返回true,否则返回false

     */

    public static boolean updateParentidAndLevelById(int parent_id,int level,int id){

        String sql="update category set parent_id=?,level=? where id=?";

        Object[] para=new Object[]{parent_id,level,id};

        if(DataConnection.request(sql,para)){

            return true;

        }

        return false;

    }





    /**

     * 根据分类id获取相应分类的信息

     * @param id 分类id

     * @return 有则返回指定分类的信息，否则返回null

     */

    public  static Category selectByPrimaryKey(Integer id){

        String sql="select id, " +

                "parent_id, " +

                "name, " +

                "status, " +

                "level, " +

                "create_time, " +

                "update_time  " +

                "from category " +

                "where id=?";

        Object[] para=new Object[]{id};

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,para);

        if(list != null && list.size()>0){

            return list.get(0);

        }

        return null;

    }



    /**

     * 分页显示第pagesize页中pagenum条记录。

     * @param pagenum  第一页显示的记录个数

     * @param pagesize  表示第几页

     * @return  存在返回记录，否则返回null

     */

    public  static  List<Category> selectCategory(int pagenum,int pagesize){

        String sql="select id, " +

                "parent_id, " +

                "name, " +

                "status, " +

                "level, " +

                "create_time, " +

                "update_time  " +

                "from category limit " +

                (pagenum-1)*pagesize+","+pagesize;

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,null);

        if(list != null && list.size()>0){

            return list;

        }

        return null;

    }



    public  static  List<Category> selectCategory(String query,int pagenum,int pagesize){

        String sql="select id, " +

                "parent_id, " +

                "name, " +

                "status, " +

                "level, " +

                "create_time, " +

                "update_time  " +

                "from category  " +

                "where name like " +

                "'%"+query+"%'  limit " +

                (pagenum-1)*pagesize+","+pagesize;

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,null);

        if(list != null && list.size()>0){

            return list;

        }

        return null;

    }



    /**

     * 根据分类的父类id,获取所有子类分类的信息

     * @param parentId 父类分类id

     * @return 成功返回指定父类id对应分类，否则返回null

     */

    public  static  List<Category> selectCategoryChildrenByParentId(Integer parentId ){

        String sql="select id, " +

                "parent_id, " +

                "name, " +

                "status, " +

                "level, " +

                "create_time, " +

                "update_time  " +

                "from category " +

                "where parent_id=? ";

        Object[] para=new Object[]{parentId};

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,para);

        if(list != null && list.size()>0){

            return list;

        }

        return null;

    }





    /**

     * 统计商品分类中所有的记录总数

     * @return 若存在记录返回记录总数，否则返回0

     */

    public  static  int  getRecordCount(){

        String sql="select id " +

                "from category " ;

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,null);

        if(list != null && list.size()>0){

            return list.size();

        }

        return 0;

    }



    /**

     * 统计包含商品分类名的记录总数

     * @param query 要统计的分类名称

     * @return 若存在返回记录总数，否则返回0

     */

    public  static  int  getRecordCount(String query){

        String sql="select id " +

                "from category " +

                "where name like '" +

                "?" +

                "'" ;

        Object[] para=new Object[]{query};

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,para);

        if(list != null && list.size()>0){

            return list.size();

        }

        return 0;

    }



    /**

     * 统计包含父类id的记录个数

     * @param parent_id  父类id

     * @return 存在返回记录个数，否则返回0

     */

    public  static  int  getRecordCount(int parent_id){

        String sql="select id " +

                "from category " +

                "where Parent_id=?" ;

        Object[] para=new Object[]{parent_id};

        List<Category> list=new DataConnection<Category>().query(Category.class,sql,para);

        if(list != null && list.size()>0){

            return list.size();

        }

        return 0;

    }



}